using System;

using Holdem.Interfaces;
using Holdem.Interfaces.Attributes.Config;
using Holdem.Interfaces.Configuration;

using Microsoft.Phone.Marketplace;

namespace Holdem.Core.Provider
{
    public class TriaProvider : ITrialProvider
    {
        private readonly IHoldemPhoneConfiguration _configuration;
        private readonly LicenseInformation _license;
        private bool _isTrial;

        #region Implementation of ITrialProvider

        public bool IsTrial
        {
            get 
            {
                if(!_configuration.ContainsKey(ConfigKey.HoldemLicenseInformationTrial))
                {
                    _isTrial = _license.IsTrial();
                    _configuration[ConfigKey.HoldemLicenseInformationTrial] = _isTrial;
                }

                return _isTrial;
            }
        }

        #endregion

        public TriaProvider([PersistentConfiguration] IHoldemPhoneConfiguration persistentConfiguration, LicenseInformation license )
        {
            if( persistentConfiguration == null )
                throw new ArgumentNullException("persistentConfiguration");
            if( license == null )
                throw new ArgumentNullException("license");

            _license = license;
            _configuration = persistentConfiguration;

            if( _configuration.ContainsKey(ConfigKey.HoldemLicenseInformationTrial) )
                _isTrial = _configuration.Cast<bool>(ConfigKey.HoldemLicenseInformationTrial);
        }
    }
}